package com.usian.controller;

import com.usian.pojo.Ygong;
import com.usian.pojo.User;
import com.usian.service.EtcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/etc")
@Controller
public class EtcController {

    @Autowired
    private EtcService etcService;


    //跳转到添加rtc的前台
    @RequestMapping("/toAddYgong")
    public String toAddYgong(){
        return "addetc";
    }

    //修改etc数据
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Model model){
        Ygong etc = etcService.findById(id);
        model.addAttribute("etc",etc);
        return "update";
    }

    //查询所有etc
    @RequestMapping("/findAll")
     public String findAll(Model model,Ygong etc){
        List<Ygong> etcList = etcService.findAll(etc);

        model.addAttribute("etcList",etcList);
        return "index";
    }


    //添加etc
    @RequestMapping("/insertEtc")
    public void insertYgong(Ygong etc, HttpServletResponse response) throws IOException {
        etcService.insertYgong(etc);
        response.sendRedirect("/etc/findAll");
    }

    //登录
    @RequestMapping("/denglu")
    public void denglu(User user, HttpServletResponse response) throws IOException {
        User user1=etcService.denglu(user);
        if (user1!=null ){
            response.sendRedirect("/etc/findAll");
        }else {
            response.sendRedirect("/etc/shibai");
        }
    }

    //跳转到失败的前台
    @RequestMapping("/shibai")
    public String shibai() throws IOException {
        return "shibai";
    }

    //跳转到失败的前台
    @RequestMapping("/dengluyonghu")
    public String dengluyonghu() throws IOException {
        return "dengluyonghu";
    }

    //注册用户
    @RequestMapping("/adduser")
    public String adduser(User user, HttpServletResponse response) throws IOException {
        etcService.adduser(user);
        return "dengluyonghu";
    }

    //跳转到注册的前台
    @RequestMapping("/addyonghu")
    public String addyonghu() throws IOException {
        return "addyonghu";
    }
//0op-loo-[
    //回显
    @RequestMapping("/deleteById")
    public void deleteById(Integer id,HttpServletResponse response) throws IOException {
        etcService.deleteById(id);
        response.sendRedirect("/etc/findAll");
    }


    //修改
    @RequestMapping("update")
    public void update(Ygong etc,HttpServletResponse response) throws IOException {
        etcService.update(etc);
        response.sendRedirect("/etc/findAll");
    }


    //导入
    @RequestMapping("/upioad")
    public void upioad(HttpServletResponse response, MultipartFile file) throws IOException {
        //  POIUtils.readExcel(file);
        response.sendRedirect("/etc/findAll");
    }

}
